//
//  CMDBInviteViewController.swift
//  CMDB
//
//  Created by 王磊 on 25/05/2017.
//  Copyright © 2017 CMDB. All rights reserved.
//

import UIKit
private var CMDB_Invite_Margin: CGFloat = (CMDB_Screen_Width - 630 * CMDB_Rate) * 0.5
class CMDBInviteViewController: NormalTitleViewController {
    
    private var sendItem: UIButton = UIButton(type: .Custom)
    
    private var textLabel: UILabel = UILabel()
    
    internal var phone: CMDBSubRegisterTF = CMDBSubRegisterTF()
    
    internal var notice: UILabel = UILabel()
    
    override func setup() {
        super.setup()
        
        buildSubviews()
        
        buildSendItem()
    }
    
    var user: CMDBUserInfoBean?
}

extension CMDBInviteViewController {
    
    private func buildSubviews() {
        
        textLabel.numberOfLines = 0
        
        if let user = user {
            
            let para = NSMutableParagraphStyle()
            
            para.lineSpacing = 5
            
            textLabel.setAttr(NSAttributedString(string: "您正在邀请", attributes: [NSFontAttributeName:CMDB_15_FONT,NSForegroundColorAttributeName: word54Color,NSParagraphStyleAttributeName: para]))
            textLabel.appendAttr(NSAttributedString(string: user.name, attributes: [NSFontAttributeName:CMDB_15_FONT,NSForegroundColorAttributeName: UIColor.Hex(0xE2E2E2),NSParagraphStyleAttributeName: para]))
            textLabel.appendAttr(NSAttributedString(string: "加入CMDB,请告知对方并准确填写被邀请人将来用于注册的手机号,系统会把邀请码以短信形式发送到对方手机。", attributes: [NSFontAttributeName:CMDB_15_FONT,NSForegroundColorAttributeName: word54Color,NSParagraphStyleAttributeName: para]))
        }
        
        contentView!.addSubview(textLabel)
        
        contentView!.addSubview(phone)
        
        let Left_Margin: CGFloat = (CMDB_Screen_Width - 630 * CMDB_Rate) * 0.5
        
        let textAttr = [NSFontAttributeName: CMDB_Register_TF_FONT,NSForegroundColorAttributeName: word87Color]
        
        let placeHolderAttr = [NSFontAttributeName: CMDB_Register_TF_FONT,NSForegroundColorAttributeName: CMDB_Register_TF_PlaceHolder_Color]
        
        phone.attrText = NSMutableAttributedString(string: "手机号码:", attributes: textAttr)
        
        phone.placeHolderAttr = NSAttributedString(string: "+86", attributes: placeHolderAttr)
        
        phone.keyboardType = .NumberPad
        
        phone.returnKeyType = .Done
        
        phone.maxLength = 11
        
        contentView!.addSubview(notice)
        
        notice.setAttr(NSAttributedString(string: "*暂不支持中国大陆以外的手机号码", attributes: [NSFontAttributeName:CMDB_15_FONT,NSForegroundColorAttributeName: word54Color]))
        
        textLabel.snp_makeConstraints { (make) in
            
            make.left.equalTo(10)
            
            make.right.equalTo(-10)
            
            make.top.equalTo(10)
        }
        
        phone.snp_makeConstraints { [weak self] (make) in
            
            make.left.equalTo(Left_Margin)
            
            make.right.equalTo(-Left_Margin)
            
            make.top.equalTo(self!.textLabel.snp_bottom)
            
            make.height.equalTo(60)
        }
        notice.snp_makeConstraints { [weak self] (make) in
            
            make.left.equalTo(Left_Margin)
            
            make.right.equalTo(-Left_Margin)
            
            make.top.equalTo(self!.phone.snp_bottom)
            
        }
        
    }
    
    private func buildSendItem() {
        
        contentView!.addSubview(sendItem)
        
        sendItem.setTitle("发送", forState: .Normal)
        sendItem.setTitle("发送", forState: .Highlighted)
        
        sendItem.setTitleColor(UIColor.Hex6(0xffffff, alpha: 0.54), forState: .Highlighted)
        
        sendItem.setTitleColor(UIColor.Hex6(0xffffff), forState: .Normal)
        
        sendItem.backgroundColor = UIColor.Hex6(0xff6d00, alpha: 54)
        
        sendItem.titleLabel!.font = CMDB_17_FONT
        
        sendItem.addTarget(self, action: #selector(sendTriggered), forControlEvents: .TouchUpInside)
        
        sendItem.layer.cornerRadius = 5
        
        sendItem.snp_makeConstraints { [weak self] (make) in
            
            make.left.equalTo(CMDB_Invite_Margin)
            
            make.right.equalTo(-CMDB_Invite_Margin)
            
            make.height.equalTo(88 * CMDB_Rate)
            
            make.top.equalTo(self!.notice.snp_bottom).offset(40)
        }
    }
}
extension CMDBInviteViewController {
    
    @objc private func sendTriggered() {
        
        dataBlock!(nil,nil,nil)
        
        navigationController?.popViewControllerAnimated(true)
        
    }
}
